<template>
  <div class="tmpl">

    <transition @befornEnter="befornEnter" @enter="enter" @afterEnter="afterEnter">
      <div class="ball" v-if="isShow"></div>
    </transition>

    <!-- 轮播图区域 -->
    <div class="mui-card">
      <div class="mui-card-content">
        <div class="mui-card-content-inner">
          <!-- 轮播图组件 -->
          <slider :imgs="lunboList"></slider>
        </div>
      </div>
    </div>

    <!-- 商品购买区域 -->
    <div class="mui-card">
      <div class="mui-card-header">{{goodsInfo.title}}</div>
      <div class="mui-card-content">
        <div class="mui-card-content-inner">

          <div class="price">
            <span>市场价：
              <del>￥{{goodsInfo.market_price}}</del>
            </span>&nbsp;&nbsp;&nbsp;&nbsp;
            <span>销售价：
              <span class="sale_price">￥{{goodsInfo.sell_price}}</span>
            </span>
          </div>

          <div class="count">
            <span>购买数量：</span>

            <!-- 数字框子组件 -->
            <numberbox :maxnum="goodsInfo.stock_quantity" @getCount="getCount"></numberbox>

          </div>


          <div class="btns">
            <mt-button type="primary" size="small">立即购买</mt-button>
            <mt-button 
            type="danger" 
            size="small" 
            @click="addToCart"
            >加入购物车</mt-button>
          </div>

        </div>
      </div>
    </div>

    <!-- 商品参数区域 -->
    <div class="mui-card">
      <div class="mui-card-header">商品参数</div>
      <div class="mui-card-content">
        <div class="mui-card-content-inner">
          <p>商品货号：{{goodsInfo.goods_no}}</p>
          <p>库存情况：{{goodsInfo.stock_quantity}}件</p>
          <p>上架时间：{{goodsInfo.add_time | dateFormat}}</p>
        </div>
      </div>
      <div class="mui-card-footer">
        <mt-button type="primary" size="large" plain @click="toGoodsDesc(id)">图文介绍</mt-button>
        <br>
        <mt-button type="danger" size="large" plain @click="toGoodsComment(id)">商品评论</mt-button>
      </div>
    </div>

  </div>
</template>


<script>
// 导入轮播图组件
import slider from '../common/slider.vue'
// 导入全局配置对象
import config from '../../js/globalConfig.js'
// 导入 数字选择框组件
import numberbox from '../common/numberbox.vue'

export default {
  data() {
    return {
      id:this.$route.params.id,
      imgBaseUrl:config.imgBaseUrl,
      lunboList:[],
      goodsInfo:{},
      goodsCount:1,  //初始数量
      isShow:false
    }
  },
  created() {
    // 获取商品详情
    this.getGoodsImg();
    this.getGoodsInfo();
  },
  methods: {
    getGoodsImg() {
      // 与缩略图公用API
      this.$http.get("api/getthumimages/"+this.id).then(res=>{
        // console.log(res.body);
        if(res.body.status === 0) {
          res.body.message.map(val=>{
            // 遍历数组添加img属性
            val.img = this.imgBaseUrl + val.src;
          })
          this.lunboList =  res.body.message;
        }
      })
    },
    getGoodsInfo() {
      this.$http.get("api/goods/getinfo/"+this.id).then(res=>{
        // console.log(res.body);
        if(res.body.status === 0) {
          this.goodsInfo = res.body.message[0];
        }
      })
    },
    toGoodsDesc(id) {
      this.$router.push("/home/goodsDes/"+id);
    },
    toGoodsComment(id) {
      this.$router.push("/home/goodsComment/"+id);
    },
    getCount(n) {
      // console.log(n);
      this.goodsCount = n;
    },
    befornEnter(el) {
      el.style.transform = "tranlate(0,0)"
    },
    enter(el,done) {
      el.offsetWidth;
      el.style.transform =  "translate(200px, " + (140 + window.pageYOffset) + "px)";
      done();
    },
    afterEnter(el) {
      this.isShow = !this.isShow;
    },
    addToCart() {
      this.isShow = !this.isShow;
      // 怼出一个商品对象，将其存入store中
      var goods = {
        // 转化为数值类型
        id : parseInt(this.$route.params.id),
        count: parseInt(this.goodsCount)
      }
      // console.log(goods);
      this.$store.commit('addToCart',goods);
    }
  },
  // 将轮播图组件注册为子组件
  components:{slider,numberbox}
}
</script>


<style lang="scss" scoped>
.tmpl {
  background-color: #EEE;
  overflow: hidden;
}

.sale_price {
  color: red;
  font-size: 18px;
}

.mui-card-content-inner>div {
  margin: 10px 0;
}

.mui-card-footer {
  display: block;
}
// 球
.ball {
  background-color: red;
  width: 15px;
  height: 15px;
  border-radius: 50%;
  position: absolute;
  left: 150px;
  top: 416px;
  z-index: 99;
  transition: all .5s cubic-bezier(.29, -0.38, 1, .68);
}
</style>